package com.ywt;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.ywt.act.RegisterActivity;
import com.ywt.main.R;
import com.ywt.tools.Action;
import com.ywt.utils.AsyncDataLoader;
import com.ywt.utils.Utils;
import com.ywt.utils.AsyncDataLoader.Callback;

public class LoginActivity extends Activity {
	private EditText mUser; // 帐号编辑框
	private EditText mPassword; // 密码编辑框

	private Action action;
	
	private String userName =null;
	private String password = null;
	
	public static void actionLogin(Context context) {
		Intent i = new Intent(context, LoginActivity.class);
		context.startActivity(i);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		mUser = (EditText) findViewById(R.id.login_user_edit);
		mPassword = (EditText) findViewById(R.id.login_passwd_edit);

		action = new Action(LoginActivity.this);
	}

	public void login_action(View v) {
		
		
		userName = mUser.getText().toString().trim();
		password = mPassword.getText().toString().trim();
		
		
  if ("".equals(userName) || "".equals(password)||userName==null||password==null) // 判断 帐号和密码
		{
			new AlertDialog.Builder(LoginActivity.this)
					.setIcon(
							getResources().getDrawable(
									R.drawable.login_error_icon))
					.setTitle("登录错误").setMessage("帐号或者密码不能为空，\n请输入后再登录！")
					.create().show();
		} else {
			action.startProgressDialog(LoginActivity.this, "正在登陆中");
			new AsyncDataLoader(asyncCallback).execute();
			

//			new AlertDialog.Builder(LoginActivity.this)
//					.setIcon(
//							getResources().getDrawable(
//									R.drawable.login_error_icon))
//					.setTitle("登录失败").setMessage("帐号或者密码不正确，\n请检查后重新输入！")
//					.create().show();
		}
	}

	// 正常异步加载
		private Callback asyncCallback = new AsyncDataLoader.Callback() {

			@Override
			public void onStart() {
				Utils.loginModes = action.LoginAction(userName, password);
			}

			@Override
			public void onPrepare() {
			}

			@Override
			public void onFinish() {

				action.stopProgressDialog();
				if (Utils.loginModes!=null) {
					action.showLongToast("登陆成功");
					LoginActivity.this.finish();
				}else action.showLongToast("登陆失败");
				
				
			}
		};
	
	public void login_back(View v) { // 标题栏 返回按钮
		this.finish();
	}
	public void login_Register(View v) { //   注册按钮
		Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
		startActivity(intent);
	}
	
	public void login_pw(View v) { // 忘记密码按钮
		Uri uri = Uri.parse("http://m.hao123.com");
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}
}
